<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sms
{
    /*
     *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
     *
     *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
     *  that can be found in the LICENSE file in the root of the web site.
     *
     *   http://www.yuntongxun.com
     *
     *  An additional intellectual property rights grant can be found
     *  in the file PATENTS.  All contributing project authors may
     *  be found in the AUTHORS file in the root of the source tree.
     */
    /**
     * 发送模板短信
     * @param to 手机号码集合,用英文逗号分开
     * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
     * @param $tempId 模板Id
     */
    function sendTemplateSMS($to, $datas, $tempId)
    {
        //主帐号
        $accountSid = '8a216da85d7dbf78015d82daba8d0231';

//主帐号Token
        $accountToken = 'd9f9ac92caff48978d56aa16400efedd';

//应用Id
        $appId = '8a216da85d7dbf78015d82dabe240237';
//echo $appId;
//请求地址，格式如下，不需要写https://
        $serverIP = 'app.cloopen.com';
//请求端口
        $serverPort = '8883';
        $Batch = date("YmdHis");
        $BodyType = "xml";
//REST版本号
        $softVersion = '2013-12-26';
//var_dump($tempId);exit;
        // 拼接请求包体
        if ($BodyType == "json") {
            $data = "";
            for ($i = 0; $i < count($datas); $i++) {
                $data = $data . "'" . $datas[$i] . "',";
            }
            $body = "{'to':'$to','templateId':'$tempId','appId':'$appId','datas':[" . $data . "]}";
            //var_dump($body);exit;
        } else {
            $data = "";
            for ($i = 0; $i < count($datas); $i++) {
                $data = $data . "<data>" . $datas[$i] . "</data>";
            }
            $body = "<TemplateSMS>
                    <to>$to</to> 
                    <appId>$appId</appId>
                    <templateId>$tempId</templateId>
                    <datas>" . $data . "</datas>
                  </TemplateSMS>";
        }
        //s$this->showlog("request body = ".$body);
        // 大写的sig参数
        $sig = strtoupper(md5($accountSid . $accountToken . $Batch));
        // 生成请求URL
        $url = "https://$serverIP:$serverPort/$softVersion/Accounts/$accountSid/SMS/TemplateSMS?sig=$sig";
        //var_dump($url);exit;
        //$this->showlog("request url = ".$url);
        // 生成授权：主帐户Id + 英文冒号 + 时间戳。
        $authen = base64_encode($accountSid . ":" . $Batch);
        // 生成包头
        $header = array("Accept:application/$BodyType", "Content-Type:application/$BodyType;charset=utf-8", "Authorization:$authen");
        // 发送请求
        $result = $this->curl_post($url, $body, $header);
        //var_dump();
        //$this->showlog("response body = ".$result);
        if ($BodyType == "json") {//JSON格式
            $datas = json_decode($result);
        } else { //xml格式
            $datas = simplexml_load_string(trim($result, " \t\n\r"));
        }
        if ($datas->statusCode == 0) {
            if ($BodyType == "json") {
                $datas->TemplateSMS = $datas->templateSMS;
                unset($datas->templateSMS);
            }
        }

        return $datas;
        //var_dump($datas);exit;
    }

//
    function curl_post($url, $data, $header, $post = 1)
    {
        //初始化curl
        $ch = curl_init();
        //参数设置
        $res = curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if ($post)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        $result = curl_exec($ch);

//        var_dump($result);
        //连接失败
        $BodyType='';
        if ($result == FALSE) {
            if ($BodyType == 'json') {
                $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";
            } else {
                $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>";
            }
        }

        curl_close($ch);
        //var_dump($result);
        return $result;
    }
}

?>